package com.sky.service.impl;


import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;


@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 添加购物车
     *
     * @param dto
     * @return
     */
    @Override
    public Result add(ShoppingCartDTO dto) {

        //判断当前商品是否在购物车内
        ShoppingCart shoppingcart = new ShoppingCart();
        BeanUtils.copyProperties(dto, shoppingcart);
        shoppingcart.setUserId(BaseContext.getCurrentId());
        shoppingcart = shoppingCartMapper.selectShoppingCart(shoppingcart);
        if (shoppingcart!=null){
            shoppingcart.setNumber(shoppingcart.getNumber()+1);
            shoppingCartMapper.updateNumber(shoppingcart);

        }else {
            shoppingcart=new ShoppingCart();
            Long dishId = dto.getDishId();
            if (dishId!=null){//说明是菜品
                Dish dish = dishMapper.selectById(dishId);
                shoppingcart.setDishFlavor(dto.getDishFlavor());
                shoppingcart.setDishId(dishId);
                shoppingcart.setAmount(dish.getPrice());
                shoppingcart.setImage(dish.getImage());
                shoppingcart.setName(dish.getName());

            }else {
                Setmeal setmeal = setmealMapper.getById(dto.getSetmealId());
                shoppingcart.setSetmealId(setmeal.getId());
                shoppingcart.setName(setmeal.getName());
                shoppingcart.setAmount(setmeal.getPrice());
                shoppingcart.setImage(setmeal.getImage());

            }
            shoppingcart.setNumber(1);
            shoppingcart.setCreateTime(LocalDateTime.now());
            shoppingcart.setUserId(BaseContext.getCurrentId());
            shoppingCartMapper.insert(shoppingcart);

        }
        return Result.success();
    }

    /**
     * 查看购物车
     *
     * @return
     */
    @Override
    public Result<List<ShoppingCart>> list() {

        Long userId = BaseContext.getCurrentId();
        List<ShoppingCart> list = shoppingCartMapper.list(userId);
        return Result.success(list);
    }

    /**
     * 清空购物车
     *
     * @return
     */
    @Override
    public Result clean() {
        shoppingCartMapper.clean(BaseContext.getCurrentId());
        return Result.success();
    }

    /**
     * 购物车商品删减
     *
     * @param dto
     * @return
     */
    @Override
    public Result subtract(ShoppingCartDTO dto) {
        ShoppingCart shoppingCart =new ShoppingCart();
        BeanUtils.copyProperties(dto,shoppingCart);
        shoppingCart = shoppingCartMapper.selectShoppingCart(shoppingCart);
        if (shoppingCart.getNumber()==1){
            Long id = shoppingCart.getId();
            shoppingCartMapper.delete(id);
        }{
            shoppingCart.setNumber(shoppingCart.getNumber()-1);
            shoppingCartMapper.updateNumber(shoppingCart);
        }


        return Result.success();
    }
}
